package com.alibaba.day0318yk.controller;


import com.alibaba.day0318yk.pojo.User;
import com.alibaba.day0318yk.service.KcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: Wang
 * @create: 2024-03-18 09:23
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private KcService kcService;

    @RequestMapping("/toLogin")
    private String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    private Map<String,Object> login(@RequestBody User user, HttpServletRequest request){
        HashMap<String, Object> map = new HashMap<>();

        User loginUser = kcService.login(user);

        if (loginUser != null){
            map.put("status",true);
            map.put("msg","/kc/list");
            request.getSession().setAttribute("user",loginUser);
        }else {
            map.put("status",false);
            map.put("msg","用户名或密码异常");
        }

        return map;

    }
}
